import Vue from 'vue'
import Router from 'vue-router'
import util from '@/utils/utils'

const _import = require('./import-production')
Vue.use(Router)

import Home from '../views/home/index.vue'

let router = new Router({
    routes: [
        {
            path: '/',
            name: 'index',
            meta: {
                isKeepAlive: true,
                tabBar: true
            },
            component: Home
        },
        {
            path: '/login',
            name: 'login',
            meta: {
                hideHeader: true,
                hideFixMenu: true,
                title: '登陆'
            },
            component: _import('login/index')
        },
        {
            path: '/login/other',
            name: 'other-login',
            meta: {
                hideHeader: true,
                hideFixMenu: true,
                title: '登陆'
            },
            component: _import('login/other')
        },
        {
            path: '/register',
            name: 'register',
            meta: {
                headerTitle: '用户注册',
                hideFixMenu: true
            },
            component: _import('register/index')
        },
        {
            path: '/password',
            name: 'password',
            meta: {
                headerTitle: '忘记密码',
                hideFixMenu: true
            },
            component: _import('password/index')
        },
        {
            path: '/news',
            name: 'news',
            meta: {
                title: '新闻公告'
            },
            component: _import('news/list')
        },
        {
            path: '/news/:id',
            name: 'news-detail',
            meta: {
                title: '新闻详情'
            },
            component: _import('news/detail')
        },
        {
            path: '/goods',
            name: 'goods',
            meta: {
                title: '商品列表',
                isKeepAlive: true,
                tabBar: true
            },
            component: _import('goods/list')
        },
        {
            path: '/consult',
            name: 'consult',
            meta: {
                title: '我要咨询'
            },
            component: _import('consult/index')
        },
        {
            path: '/help',
            name: 'help',
            meta: {
                title: '常见问题'
            },
            component: _import('help/index')
        },
        {
            path: '/admin',
            name: 'admin-home',
            meta: {
                title: '个人中心',
                tabBar: true
            },
            component: _import('admin/index'),
        },
        {
            path: '/admin/user',
            name: 'admin-user',
            meta: {
                title: '用户管理'
            },
            component: _import('admin/user'),
        },
        {
            path: '/admin/user-info',
            name: 'admin-user-info',
            meta: {
                title: '用户信息'
            },
            component: _import('admin/user-info'),
        },
        {
            path: '/admin/order',
            name: 'admin-order',
            meta: {
                title: '我的订单',
                tabBar: true
            },
            component: _import('admin/order'),
        },
        {
            path: '/admin/order/:id',
            name: 'admin-order-detail',
            meta: {
                title: '订单详情'
            },
            component: _import('admin/order-detail'),
        },
        {
            path: '/admin/account',
            name: 'admin-account',
            meta: {
                title: '账号信息'
            },
            component: _import('admin/account'),
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        // 从第二页返回首页时savedPosition为undefined
        if (savedPosition || typeof savedPosition === 'undefined') {
            // 只处理设置了路由元信息的组件
            from.meta.isKeepAlive = typeof from.meta.isKeepAlive === 'undefined' ? undefined : false
            to.meta.isKeepAlive = typeof to.meta.isKeepAlive === 'undefined' ? undefined : true
            if (savedPosition) {
                return savedPosition
            }
        } else {
            from.meta.isKeepAlive = typeof from.meta.isKeepAlive === 'undefined' ? undefined : true
            to.meta.isKeepAlive = typeof to.meta.isKeepAlive === 'undefined' ? undefined : false
        }
    }
})

// 后置
router.afterEach((to, from) => {
    let title = to.meta.title ? `$TEXT$网-${to.meta.title}` : '$TEXT$网'
    util.setTitle(title)
    document.documentElement.scrollTop = 0
    document.body.scrollTop = 0
    window.scrollTop = 0
    // document.querySelector('#app').scrollTop = 0
})
export default router
